package syn;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//测试lock锁
public class test5 {

	public static void main(String[] args) {
		test5lock test5lock=new test5lock();
		
		new Thread(test5lock).start();
		new Thread(test5lock).start();
		new Thread(test5lock).start();
	}
}

class test5lock implements Runnable {

	int ticketNums = 10;

	private final ReentrantLock Lock=new  ReentrantLock();	
	@Override
	public void run() {
		while (true) {
			Lock.lock();
			try {
				if (ticketNums > 0) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					System.out.println(ticketNums--);
				} else {
					break;
				}
			} finally {
				Lock.unlock();
			}
			
		
		}
	}

}